package com.fzrskj.cloud.free.dic.utils;

import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.dic.bean.DicPrice;
import com.fzrskj.cloud.free.dic.dao.DicPriceDao;
import ms.core.object.CObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;
import java.util.Map;

public class DicPriceUtils extends CObject{
	
	//添加价格
	public boolean addDicPrice(String dataItem){
		JSONArray array = JSONArray.fromObject(dataItem);
		DicPriceDao dao=new DicPriceDao();
		for(int i=0;i<array.size();i++){
			JSONObject json = array.getJSONObject(i);
			DicPrice dicPrice=new DicPrice();
			dicPrice.setHostType(json.getString("hostType"));
			dicPrice.setChargeItem(json.getString("chargeItem"));
			dicPrice.setParameter(json.getString("parameter"));
			if(!CStrTool.isBlankStr(json.getString("price"))) {
				dicPrice.setPrice(Double.parseDouble(json.getString("price")));
			}

			dao.addDicPrice(dicPrice);
		}
		dao.dispose();
		return true;
	}

	//删除价格
	public int delDicPrice(Map<String, Object> cdt){
		DicPriceDao dao=new DicPriceDao();
		int ret=dao.delDicPrice(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	//查询价格列表
	public List<DicPrice> queryDicPriceList(Map<String, Object> cdt){
		DicPriceDao dao=new DicPriceDao();
		List<DicPrice> list=dao.queryDicPriceList(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return list;
	}

	//查询价格
	public double queryPrice(String hostType,String chargeItem,String parameter){
		DicPriceDao dao=new DicPriceDao();
		double price=dao.queryPrice(hostType, chargeItem, parameter);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return price;
	}

	/**
	 * 指定代理查询价格设置.
	 * @param hostType 注册产品类型.
	 * @param chargeItem 操作类型1新购2续费.
	 * @param parameter 功能项编码.
	 * @param agentId 代理ID.
	 * @return 价格.
	 */
	public double queryPriceAppointAgent(String hostType,String chargeItem,String parameter,Integer agentId){
		DicPriceDao dao=new DicPriceDao();
		double price=dao.queryPriceAppointAgent(hostType, chargeItem, parameter, agentId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return price;
	}
}
